<?php
declare (strict_types = 1);

namespace albert\services;

/**
 * 接口响应服务类
 * Class JsonService
 * @package albert\services
 */
class JsonService
{
    private static $SUCCESSFUL_DEFAULT_MSG = 'ok';

    private static $FAIL_DEFAULT_MSG = 'no';

    /**
     * JSON 返回数据
     * Author: Albert3306
     * Date: 2021/4/28
     * @param $code
     * @param string $msg
     * @param array $data
     * @param int $count
     * @return string
     */
    public static function result($code, string $msg = '', array $data = [], int $count = 0): string
    {
        exit(json_encode(compact('code', 'msg', 'data', 'count')));
    }

    /**
     * LayUI 成功返回
     * Author: Albert3306
     * Date: 2021/4/28
     * @param int $count
     * @param array $data
     * @param string $msg
     * @return string
     */
    public static function successLayUI(int $count = 0, array $data = [], string $msg = ''): string
    {
        if (is_array($count)) {
            if (isset($count['data'])) $data = $count['data'];
            if (isset($count['count'])) $count = $count['count'];
        }
        if (false == is_string($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        return self::result(0, $msg, $data, $count);
    }

    /**
     * 成功返回 - 可设置响应状态码
     * Author: Albert3306
     * Date: 2021/4/28
     * @param string|array $msg
     * @param array $data
     * @param int $status
     * @return string
     */
    public static function successful($msg = 'ok', array $data = [], int $status = 200): string
    {
        if (false == is_string($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        return self::result($status, $msg, $data);
    }

    /**
     * 状态返回
     * Author: Albert3306
     * Date: 2021/4/28
     * @param $status
     * @param $msg
     * @param array $result
     * @return string
     */
    public static function status($status, $msg, array $result = []): string
    {
        $status = strtoupper($status);
        if (true == is_array($msg)) {
            $result = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        return self::result(200, $msg, compact('status', 'result'));
    }

    /**
     * 失败返回
     * Author: Albert3306
     * Date: 2021/4/28
     * @param $msg
     * @param array $data
     * @param int $code
     * @return string
     */
    public static function fail($msg, array $data = [], int $code = 400): string
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$FAIL_DEFAULT_MSG;
        }
        return self::result($code, $msg, $data);
    }

    /**
     * 成功返回
     * Author: Albert3306
     * Date: 2021/4/28
     * @param $msg
     * @param array $data
     * @return string
     */
    public static function success($msg, array $data = []): string
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        return self::result(200, $msg, $data);
    }
}
